package com.example.act6.config;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.act6.assist.ActeBpmnJsonConverter;

import com.example.act6.assist.GlobalEventListener;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ActivitiProperties;
import org.activiti.spring.boot.DataSourceProcessEngineAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * activiti配置类
 *
 * @author
 * @date 2021/8/9
 */
@Configuration
@EnableConfigurationProperties(ActivitiProperties.class)
@AutoConfigureAfter(value = {
        DataSourceAutoConfiguration.class,
        GlobalEventListener.class
})
@AutoConfigureBefore(DataSourceProcessEngineAutoConfiguration.class)
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration implements WebMvcConfigurer {

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
            DataSource dataSource,
            PlatformTransactionManager transactionManager,
            SpringAsyncExecutor springAsyncExecutor,
            GlobalEventListener globalEventListener) throws IOException {
        SpringProcessEngineConfiguration configuration = this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
        configuration.setIdGenerator(IdWorker::getIdStr);
        configuration.setActivityFontName("宋体");
        configuration.setAnnotationFontName("宋体");
        configuration.setLabelFontName("宋体");
        List<ActivitiEventListener> eventListenerList = new LinkedList<>();
        eventListenerList.add(globalEventListener);
        configuration.setEventListeners(eventListenerList);
//        configuration.setProcessDiagramGenerator(new ActeProcessDiagramGenerator());
        configuration.setProcessEngineName("PlantmateProcessEngine");
        //设置行为工厂
//        configuration.setActivityBehaviorFactory(new ActeActivityBehaviorFactory());
        return configuration;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/");
    }

//    @Bean
//    public BpmnJsonConverter bpmnJsonConverter() {
//        return new ActeBpmnJsonConverter();
//    }
}
